package com.typing.alarm.activity;

import java.util.Calendar;
import java.util.GregorianCalendar;


import com.typing.alarm.R;
import com.typing.alarm.receiver.TypingAlarmReceiver;
import com.typing.alarm.util.DialogUtil;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class TypingAlarmActivity extends Activity {
	private AlarmManager mgr;
	private GregorianCalendar cal;
	private TimePicker time;
	private boolean[] repeatDay;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        cal = new GregorianCalendar();
        setContentView(R.layout.alarm_register);
        repeatDay = new boolean[7];
        Button choose = (Button)findViewById(R.id.choose_day);
        choose.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showChooseDay();
			}
		});
        
        Button save = (Button)findViewById(R.id.save_alarm);
        save.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setAlarm();
			}
		});
        
        Button cancel = (Button)findViewById(R.id.cancel_alarm);
        cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				killAlarm();
			}
		});
        
        
        
        time = (TimePicker)findViewById(R.id.alarm_time);
        time.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
        time.setCurrentMinute(cal.get(Calendar.MINUTE));
        time.setOnTimeChangedListener(new OnTimeChangedListener() {
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				// TODO Auto-generated method stub
				Log.d("TypingAlarmActivity","onTimeChanged " + cal.get(cal.YEAR) + " " +cal.get(cal.MONTH)+ " " +cal.get(cal.DATE) + " " + hourOfDay + " " + minute);
		
				cal.set(cal.get(cal.YEAR), cal.get(cal.MONTH), cal.get(cal.DATE), hourOfDay, minute);
			}
		});
    }
    public void setRepeatDay(int index,boolean isChecked){
    	repeatDay[index] = isChecked;
    }
    public boolean[] getRepeatDay(){
    	return this.repeatDay;
    }
    public void setRepeatDayByString(String str){
    	String[] tempStr = str.split(",");
    	for(int i=0 ; i < tempStr.length ; i++){
    		if("true".equals(tempStr[i])){
    			repeatDay[i] = true;
    		}else{
    			repeatDay[i] = false;
    		}
    		
    	}
    }
    public String getRepeatDayToString(){
    	String tempStr = "";
    	for(int i=0 ; i < repeatDay.length ; i++){
    		if(i != 0){
    			tempStr += ",";
    		}
    		if(repeatDay[i]){
    			tempStr += "true";
    		}else{
    			tempStr += "false";
    		}
    		
    	}
    	return tempStr;
    }
    public void showChooseDay(){
    	//DialogUtil.showChooseDay(this);
    }
    public void setAlarm(){
    	//mgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 0, pendingIntent());
    	mgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5*1000,pendingIntent());
    	Log.d("TypingAlarmActivity","set alarm compelete");
    }
    
    public void killAlarm(){
    	mgr.cancel(pendingIntent());
    	Log.d("TypingAlarmActivity","kill alarm compelete");
    }
    
    private PendingIntent pendingIntent(){
    	Intent i = new Intent(this,TypingAlarmReceiver.class);
    	PendingIntent pi = PendingIntent.getBroadcast( this, 0, i, 0);
    	return pi;
    }
}